<template>
    <div class="app-container">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['pet:appointment:add']"
          >预约</el-button>
        </el-col>
      </el-row>
  
      <el-table v-loading="loading" :data="appointmentList">
        <el-table-column label="预约编号" align="center" prop="appointmentId" />
        <el-table-column label="关联的宠物ID" align="center" prop="petId" />
        <el-table-column label="关联的医生ID" align="center" prop="doctorId" />
        <el-table-column label="预约时间" align="center" prop="appointmentTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.appointmentTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="预约状态" align="center" prop="status" />
        <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['pet:appointment:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['pet:appointment:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改预约挂号对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="选择宠物" prop="petId">
            <el-select v-model="form.petId" placeholder="请选择宠物">
              <el-option v-for="item in petList" :key="item.petId" :label="item.name" :value="item.petId" />
            </el-select>
          </el-form-item>
          <el-form-item label="选择医生" prop="doctorId">
            <el-select v-model="form.doctorId" placeholder="请选择医生">
              <el-option v-for="item in doctorList" :key="item.userId" :label="item.userName" :value="item.userId" />
            </el-select>
          </el-form-item>
          <el-form-item label="预约时间" prop="appointmentTime">
            <el-date-picker clearable
              v-model="form.appointmentTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择预约时间">
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import { listAppointment, getAppointment, delAppointment, addAppointment, updateAppointment } from "@/api/pet/appointment";
  import { listPet, getPet, delPet, addPet, updatePet } from "@/api/pet/pet";
  import { listUser, getUser, delUser, addUser, updateUser, resetUserPwd, changeUserStatus, deptTreeSelect } from "@/api/system/user";
  import Cookies from "js-cookie";
  export default {
    name: "AppointmentItem",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 预约挂号表格数据
        appointmentList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          petId: null,
          doctorId: null,
          appointmentTime: null,
          status: null,
          createdAt: null
        },
        // 宠物列表
        petList: [],
        // 医生列表
        doctorList: [],
        queryParamsPet: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          species: null,
          age: null,
          ownerId: null
        },
        queryParamsDoctor: {
          pageNum: 1,
          pageSize: 20,
          userName: undefined,
          phonenumber: undefined,
          status: undefined,
          deptId: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          petId: [
            { required: true, message: "关联的宠物ID不能为空", trigger: "blur" }
          ],
          doctorId: [
            { required: true, message: "关联的医生ID不能为空", trigger: "blur" }
          ],
          appointmentTime: [
            { required: true, message: "预约时间不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询预约挂号列表 */
      getList() {
        this.loading = true;
        listAppointment(this.queryParams).then(response => {
          this.appointmentList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
        // 获取宠物列表
        this.queryParamsPet.ownerId = Cookies.get("petUserId");
        listPet(this.queryParamsPet).then(response => {
          this.petList = response.rows;
        });
        // 获取医生列表
        this.queryParamsDoctor.deptId = 201;
        listUser(this.queryParamsDoctor).then(response => {
          this.doctorList = response.rows;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          appointmentId: null,
          petId: null,
          doctorId: null,
          appointmentTime: null,
          status: null,
          createdAt: null
        };
        this.resetForm("form");
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加预约挂号";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const appointmentId = row.appointmentId || this.ids
        getAppointment(appointmentId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改预约挂号";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.appointmentId != null) {
              updateAppointment(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addAppointment(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const appointmentIds = row.appointmentId || this.ids;
        this.$modal.confirm('是否确认删除预约挂号编号为"' + appointmentIds + '"的数据项？').then(function() {
          return delAppointment(appointmentIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
    }
  };
  </script>
  